﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Collections.Generic;
using System.Collections;
using Ccit.OA.Utils;

namespace Ccit.OA.NewsCenter
{
    public class CommentSQL:IComment
    {
        LoginUser loginUser = (LoginUser)HttpContext.Current.Session["LogUserInfo"];

        #region 根据条件获得评论信息

        public List<MsComment> GetCommentByClause(string sNewsId, string sCommentContent, string sSortType)
        {
            List<MsComment> lstComment = new List<MsComment>();
            string sSql = "SELECT * FROM NEWS_A7 WHERE NEWS_A7_50 LIKE '%" + sCommentContent + "%'";
            if (sNewsId != "")
                sSql += " AND NEWS_A7_20='" + sNewsId + "'";            
            sSql += " ORDER BY NEWS_A7_70 DESC";
            if (sSortType == "0")
                sSql += ",NEWS_A7_60 DESC";
            else if(sSortType=="1")
                sSql += ",NEWS_A7_60 ASC";
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                while (iReader.Read())
                {
                    MsComment msComment = new MsComment();
                    msComment.Id = iReader["NEWS_A7_10"].ToString();
                    msComment.NewsId = iReader["NEWS_A7_20"].ToString();
                    msComment.UserId = iReader["NEWS_A7_30"].ToString();
                    msComment.ExpressType = iReader["NEWS_A7_40"].ToString();
                    msComment.CommentContent = iReader["NEWS_A7_50"].ToString();
                    msComment.CommentDate = iReader["NEWS_A7_60"].ToString();
                    msComment.TopFlag = iReader["NEWS_A7_70"].ToString();
                    lstComment.Add(msComment);
                }
            }
            return lstComment;
        }

        #endregion

        #region 设置评论置顶


        public bool SetCommentTop(string sCommentIds)
        {
            bool bFlag = false;
            if (sCommentIds != "")
            {
                ArrayList list = new ArrayList();
                string sSql = "";
                string[] arrCommentIds = sCommentIds.Split(',');
                for (int i = 0; i < arrCommentIds.Length; i++)
                {
                    sSql = "UPDATE NEWS_A7 SET NEWS_A7_70='1' WHERE NEWS_A7_10='" + arrCommentIds[i] + "'";
                    list.Add(sSql);
                }
                if (DataAccessor.ExecuteNonQuery(list) > 0)
                    bFlag = true;
            }
            return bFlag;
        }

        #endregion

        #region 取消评论置顶状态


        public bool CancelCommentTop(string sCommentIds)
        {
            bool bFlag = false;
            if (sCommentIds != "")
            {
                ArrayList list = new ArrayList();
                string sSql = "";
                string[] arrCommentIds = sCommentIds.Split(',');
                for (int i = 0; i < arrCommentIds.Length; i++)
                {
                    sSql = "UPDATE NEWS_A7 SET NEWS_A7_70='0' WHERE NEWS_A7_10='" + arrCommentIds[i] + "'";
                    list.Add(sSql);
                }
                if (DataAccessor.ExecuteNonQuery(list) > 0)
                    bFlag = true;
            }
            return bFlag;
        }

        #endregion

        #region 删除评论


        public bool DeleteComment(string sCommentIds)
        {
            bool bFlag = false;
            if (sCommentIds != "")
            {
                ArrayList list = new ArrayList();
                string sSql = "";
                string[] arrCommentIds = sCommentIds.Split(',');
                for (int i = 0; i < arrCommentIds.Length; i++)
                {
                    sSql = "DELETE FROM NEWS_A7 WHERE NEWS_A7_10='" + arrCommentIds[i] + "'";
                    list.Add(sSql);
                }
                if (DataAccessor.ExecuteNonQuery(list) > 0)
                    bFlag = true;
            }
            return bFlag;
        }

        #endregion

        #region 发表评论


        public bool ExpressComment(MsComment msComment)
        {
            bool bFlag = false;
            if (msComment != null)
            {
                string sSql = "";
                string[] sPubMsg = Global.SessionExt.GetSysFieldSQL("NEWS_A7");
                string sNewsId = msComment.NewsId;
                string sCommentId = "";
                string sUserId = loginUser.UserId;
                string sExpType = msComment.ExpressType;
                string sCommentContent = msComment.CommentContent;
                string sCommentDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string sTopFlag = "0";
                sCommentId = DataInterface.GetMaxId("NEWS_A7", "NEWS_A7_10").ToString();
                sSql = "INSERT INTO NEWS_A7(NEWS_A7_10,NEWS_A7_20,NEWS_A7_30,NEWS_A7_40,NEWS_A7_50,NEWS_A7_60,NEWS_A7_70," + sPubMsg[0] + ") VALUES('" + sCommentId;
                sSql += "','" + sNewsId + "','" + sUserId + "','" + sExpType + "','" + sCommentContent.Replace("'", "''") + "','" + sCommentDate + "','" + sTopFlag + "'," + sPubMsg[1] + ")";
                if (DataAccessor.ExecuteNonQuery(sSql) > 0)
                    bFlag = true;
            }
            return bFlag;
        }

        #endregion
    }
}
